import inspect
import importlib
import os

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


class DictionaryMeta(type):

    def __new__(cls, name, bases, dct):
        all_classes = []
        for file_name in os.listdir(CURRENT_DIR):
            if not file_name.endswith('.py') or file_name.startswith('_'):
                continue
            module_name = file_name[:file_name.rfind('.py')]
            module = importlib.import_module(f"eval_lib.source.{module_name}")

            # 使用inspect检查类
            all_classes.extend([
                cls[1] for cls in inspect.getmembers(module, inspect.isclass)
            ])

        # 同步所有Dictionary类中的字典
        mappings = {}
        for dct_cls in all_classes:
            for attr in dir(dct_cls):
                if attr.endswith("_DICTIONARY"):
                    mappings[attr] = getattr(dct_cls, attr)
        dct.update(mappings)
        return super().__new__(cls, name, bases, dct)


class Dictionary(metaclass=DictionaryMeta):

    @classmethod
    def update(cls, key, value):
        key = f"{key.upper()}_DICTIONARY"
        if hasattr(cls, key):
            setattr(cls, key, value)
